package com.ph.irp.base.settlement.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import com.ph.irp.base.accountbook.service.AccountbookService;
import com.ph.irp.base.settlement.service.PBaseSettlementService;
import com.ph.irp.dto.SettlementDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseSettlement;

public class SettlementAction extends ActionSupport {
	// 结算方式service
	private PBaseSettlementService settlementService = null;
	// 结算方式dto
	private List<SettlementDTO> settlementDTOList = null;
	// 结算方式model
	private PBaseSettlement pbaseSettlement = null;
	// 接收选中复选框传递的数据
	private Object[] rowlist = null;
	// 会计科目model
	private PBaseAccountbook pbaseaccountbook;
	// 会计科目service
	private AccountbookService accountService;

	private String FORWORD;

	public String execute() throws Exception {
		try {
			return this.showSettlements();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 动态查询结算方式
	 * 
	 * @return String variable to locate the view
	 */
	public String showSettlements() throws Throwable {
		// System.out.println(pbaseSettlement);
		settlementDTOList = this.settlementService
				.getSettlements(pbaseSettlement);
		pbaseSettlement = null;
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 增加结算方式
	 * 
	 * @param pbaseSettlement
	 * @return
	 */
	public String addSettlement() throws Throwable {
		// 新增结算方式
		settlementService.addSettlement(pbaseSettlement);
		pbaseSettlement = null;

		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 将视图定位到新增界面
	 * 
	 * @return
	 */
	public String addInputSettlement() throws Throwable {
		pbaseSettlement = null;
		pbaseaccountbook = null;

		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 修改结算方式
	 * 
	 * @return
	 */
	public String updateSettlement() throws Throwable {
		Integer settlementId = Integer.valueOf((String) rowlist[0]);
		pbaseSettlement = settlementService.getSettlementById(settlementId);
		pbaseaccountbook = accountService.getAccountbookById(pbaseSettlement
				.getPBaseAccountbookId());
		rowlist = null;
		FORWORD = "success";
		return FORWORD;
	}

	public String deleteSettlement() throws Throwable {

		settlementService.deleteSettlements(rowlist);
		FORWORD = "success";
		return FORWORD;
	}

	public String selectSettlement() throws Throwable {

		settlementDTOList = this.settlementService
				.getSettlements(pbaseSettlement);
		pbaseSettlement = null;
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * setters and getters for instance variables
	 * 
	 * @return
	 */
	public PBaseSettlementService getSettlementService() {
		return settlementService;
	}

	public void setSettlementService(PBaseSettlementService settlementService) {
		this.settlementService = settlementService;
	}

	public List<SettlementDTO> getSettlementDTOList() {
		return settlementDTOList;
	}

	public void setSettlementDTOList(List<SettlementDTO> settlementDTOList) {
		this.settlementDTOList = settlementDTOList;
	}

	public PBaseSettlement getPbaseSettlement() {
		return pbaseSettlement;
	}

	public void setPbaseSettlement(PBaseSettlement pbaseSettlement) {
		this.pbaseSettlement = pbaseSettlement;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public PBaseAccountbook getPbaseaccountbook() {
		return pbaseaccountbook;
	}

	public void setPbaseaccountbook(PBaseAccountbook pbaseaccountbook) {
		this.pbaseaccountbook = pbaseaccountbook;
	}

	public AccountbookService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountbookService accountService) {
		this.accountService = accountService;
	}
}
